﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using com.loto.Common.DBUtility;
using System.Data.SqlClient;
using com.huigo.Framework.Model;

namespace com.huigo.Framework.BLL
{
    public class PolicyFlow
    {
        public static List<Model.PolicyFlow> GetPolicyFlowList(long policyFid)
        {
          List<string> fields = new List<string>() 
             {
                "FLOW_ID",
                "FLOW_POLICY_FID",
                "FLOW_POLICY_STATUS_FID",
                "FLOW_CREATED_ON",
                "FLOW_MEMO,USER_NAME,STATUS_DESC"
             };
            List<WhereStatement> whereList = new List<WhereStatement>();
            whereList.Add(new WhereStatement("FLOW_POLICY_FID", "=", policyFid.ToString(), ""));
            List<JoinStatement> join = new List<JoinStatement>();
            join.Add(new JoinStatement("inner", "HG_POLICY_STATUS", "STATUS_ID=FLOW_POLICY_STATUS_FID"));
            join.Add(new JoinStatement("left", "HG_USER_INFO", "FLOW_OPER_FID=USER_ID"));
            List<OrderByStatement> order = new List<OrderByStatement>();
            order.Add(new OrderByStatement("FLOW_CREATED_ON",false));
            List<Model.PolicyFlow> PolicyFlowList = null;
            PolicyFlowList = SqlServerDal.PolicyFlow.GetItems(fields, join, whereList, order, null);
            return PolicyFlowList;

        }
        public static bool AddPolicyFlow(long insType, long policyID, long operFID, int statusId, string statusMeno, SqlTransaction trans)
        {

            Model.PolicyFlow policyFlow = new Model.PolicyFlow();
            policyFlow.FlowInsTypeFid = insType;
            policyFlow.FlowPolicyFid = policyID;
            policyFlow.FlowOperFid = operFID;
            policyFlow.FlowPolicyStatusFid = statusId;
            policyFlow.FlowMemo = statusMeno;
            return SqlServerDal.PolicyFlow.Insert(policyFlow, trans);
        }
        /// <summary>
        /// 查询保单的最后一条流
        /// </summary>
        /// <param name="policyId">保单号</param>        
        /// <returns></returns>
        public static Model.PolicyFlow GetPolicyFlow(long policyId)
        {
            long insType = policyId % 1000;

            List<string> fileds = new List<string>()
            {
                "TOP 1 FLOW_ID,FLOW_INS_TYPE_FID,FLOW_OPER_FID,FLOW_POLICY_STATUS_FID,FLOW_MEMO"
            };
            List<WhereStatement> whereList = new List<WhereStatement>()
            {
                new WhereStatement("FLOW_POLICY_FID","=","'"+policyId+"'","and"),
                new WhereStatement("FLOW_INS_TYPE_FID","=",""+insType+"","")
            };
            List<OrderByStatement> orderList = new List<OrderByStatement>()
            {
                new OrderByStatement("FLOW_CREATED_ON",false)
            };
            List<Model.PolicyFlow> flowList = SqlServerDal.PolicyFlow.GetItems(fileds, null, whereList, orderList, null, null);
            if (flowList.Count > 0)
            {
                return flowList[0];
            }
            else
            {
                return null;
            }
        }
    }
}
